<template>
	<view class="flex-col page">
		<view class="note">
			<view class="title">使用须知</view>
			<view class="content">
				<view>1. 通电时间只有30分钟</view>
				<view>2. 熄灭后需要重新点击</view>
				<view>3. 人需要在教室附近才能点击通电</view>
			</view>
		</view>
		<view class="order_title">
			教室列表
		</view>
		<view class="flex-col group" v-if="roomList.length">
			<view class="section_1" v-for="item in roomList" :key="item.id">
				<view class="flex-row" style="justify-content: space-between;">
					<view class="flex-row">
						<u-image width="144" height="144" :src="item.room_bannerimage" border-radius="8"></u-image>
						<view class="flex-col group_1">
							<text class="text_3">{{ item.store_name | substring(12) }}</text>
							<text class="text_4">{{ item.room_name | substring(8) }}</text>
							<text class="text_4">{{ item.displayDistance }}</text>
						</view>
					</view>
					<view class="unlock_button" @click="$u.throttle(openElectric(item.id), 1000)">
						通电
					</view>
				</view>
			</view>
		</view>
		<view class="flex-col group" v-else>
			<view class="flex-col group_empey">
				<view class="flex-col items-center group_2">
					<image src="../../static/svgicon/no_record.svg" class="img" />
				</view>
				<text class="text_1">暂无教室</text>
			</view>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			roomList: [],
		}
	},
	onLoad() {
		// Check location permission first
		// 检查并请求位置权限
		const checkLocationPermission = async () => {
			try {
				const setting = await new Promise((resolve, reject) => {
					wx.getSetting({
						success: resolve,
						fail: reject
					});
				});

				if (!setting.authSetting['scope.userLocation']) {
					try {
						await new Promise((resolve, reject) => {
							wx.authorize({
								scope: 'scope.userLocation',
								success: resolve,
								fail: reject
							});
						});
					} catch (err) {
						await new Promise((resolve) => {
							uni.showModal({
								title: '提示',
								content: '需要获取您的地理位置，请确认授权',
								success: (res) => {
									if (res.confirm) {
										uni.openSetting({
											success: (res) => {
												if (!res.authSetting['scope.userLocation']) {
													uni.showToast({
														title: '获取地理位置失败',
														icon: 'none'
													});
												}
												resolve();
											}
										});
									} else {
										resolve();
									}
								}
							});
						});
					}
				}
			} catch (err) {
				console.error('位置权限检查失败:', err);
			}
		};

		// 获取房间列表并计算距离
		const getRoomListWithDistance = async () => {
			try {
				const res = await this.$u.api.getCleanRoomList();
				this.roomList = res.data;

				const location = await new Promise((resolve, reject) => {
					uni.getLocation({
						type: 'gcj02',
						success: resolve,
						fail: reject
					});
				});

				this.roomList = this.roomList.map(room => ({
					...room,
					distance: this.distanceM(location.latitude, location.longitude, room.latitude, room.longitude),
					displayDistance: this.distance(location.latitude, location.longitude, room.latitude, room.longitude)
				})).sort((a, b) => a.distance - b.distance);

			} catch (err) {
				console.error('获取房间列表或位置信息失败:', err);
				uni.showToast({
					title: '获取位置信息失败',
					icon: 'none'
				});
			}
		};

		// 执行初始化
		checkLocationPermission().then(() => {
			getRoomListWithDistance();
		});
	},
	methods: {
		async open_electric({
			id,
			latitude: lat,
			longitude: lng
		}) {
			try {
				const location = await new Promise((resolve, reject) => {
					uni.getLocation({
						type: 'gcj02',
						success: resolve,
						fail: reject
					});
				});

				const distance = this.distanceM(location.latitude, location.longitude, lat, lng);

				if (distance < 200) {
					this.openElectric(id);
				} else {
					uni.showToast({
						title: '你到教室距离还比较远,请到教室附近再通电',
						icon: 'none'
					});
				}
			} catch (err) {
				// console.error('获取位置信息失败');
				uni.showToast({
					title: '获取位置信息失败，请授权位置权限',
					icon: 'none'
				});
			}

		},

		openElectric(id) {
			console.log(id)
			this.$u.api.openElectric({
				room_id: id
			}).then((res) => {
				uni.showModal({
					title: '提示',
					content: res.msg,
					showCancel: false,
					success: () => {}
				});
			})
		}
	}
}
</script>

<style scoped lang="scss">
.page {
	.note {
		padding: 24rpx;

		.title {
			font-weight: 500;
			font-size: 32rpx;
			color: #000000;
			text-align: center;
			padding: 24rpx 0 40rpx;
		}

		.content {
			white-space: pre-line;
			font-style: normal;
			font-weight: 400;
			font-size: 28rpx;
			line-height: 50rpx;
			color: #000000;
		}

	}

	.order_title {
		font-style: normal;
		font-weight: 500;
		font-size: 32rpx;
		line-height: 20px;
		color: #000000;
		margin-top: 40rpx;
		padding-left: 24rpx;
	}

	.unlock_button {
		width: 100rpx;
		height: 100rpx;
		border-radius: 50%;
		background: linear-gradient(135deg, #E85389 15.23%, #E25588 84.77%);
		color: white;
		font-style: normal;
		font-weight: 500;
		font-size: 14px;
		line-height: 100rpx;
		text-align: center;
		margin-top: 26rpx;
		margin-right: 20rpx;
	}

	background-color: #ffffff;
	width: 100%;
	overflow-y: auto;
	height: 100%;

	.group_empey {
		color: rgb(51, 51, 51);
		font-size: 28rpx;
		font-weight: 200;
		line-height: 40rpx;
		white-space: nowrap;
		padding-top: 266rpx;

		.group_2 {
			position: relative;
			align-items: center;

			.img {
				width: 150rpx;
				height: 188rpx;
			}
		}

		.text_1 {
			margin-top: 54rpx;
			align-self: center;
		}
	}

	.button {
		margin-top: 52rpx;
		padding: 14rpx 0 16rpx;
		align-self: center;
		color: rgb(255, 255, 255);
		font-size: 36rpx;
		font-weight: 600;
		line-height: 50rpx;
		white-space: nowrap;
		background-color: rgb(216, 88, 134);
		border-radius: 8rpx;
		width: 300rpx;
	}

	.group {
		padding: 0rpx 22rpx 20rpx 24rpx;
		flex: 1 1 auto;
		overflow-y: auto;

		.button {
			border-radius: 8rpx;
			border: solid 4rpx rgb(216, 88, 134);
			color: #FFFFFF;
			background-color: rgb(216, 88, 134);
			font-size: 28rpx;
			font-weight: 600;
			line-height: 40rpx;
			align-items: center;
		}

		.hoverClass {
			background-color: rgba(216, 88, 134, 0.2);
			color: rgb(216, 88, 134);
			border: solid 4rpx rgb(216, 88, 134);
		}

		.section_1 {
			padding: 30rpx 22rpx;
			background-color: rgb(255, 255, 255);
			box-shadow: 0px 4rpx 8rpx 0px rgb(238, 235, 236);
			border-radius: 16rpx;
			border: solid 2rpx rgb(236, 236, 236);
			margin-top: 30rpx;

			.group_1 {
				margin-left: 32rpx;
				flex: 1 1 auto;

				.text_3 {
					color: rgb(51, 51, 51);
					font-size: 34rpx;
					font-weight: 600;
					line-height: 40rpx;
					white-space: nowrap;
				}

				.text_4 {
					margin-top: 12rpx;
					color: rgb(51, 51, 51);
					font-size: 32rpx;
					font-weight: 200;
					line-height: 40rpx;
					white-space: nowrap;
				}

				.text_5 {
					margin-top: 12rpx;
					color: rgb(51, 51, 51);
					font-size: 28rpx;
					font-weight: 200;
					line-height: 40rpx;
					white-space: nowrap;
				}

				.group_2 {
					margin-top: 12rpx;
					color: rgb(51, 51, 51);
					font-size: 28rpx;
					font-weight: 600;
					line-height: 40rpx;
					white-space: nowrap;

					.text_7 {
						margin-left: 12rpx;
					}

					.image_3 {
						margin: 0rpx 0 0rpx 24rpx;
						width: 40rpx;
						height: 36rpx;
					}
				}

				.equal-division {
					margin-top: 32rpx;

					.equal-division-item {
						border: none;
						padding: 4rpx 15rpx 6rpx;
						flex: 1 1 208rpx;
						border-radius: 8rpx;
						border: solid 4rpx rgb(216, 88, 134);
						color: rgb(216, 88, 134);
						font-size: 28rpx;
						font-weight: 600;
						line-height: 40rpx;
						background-color: #FFFFFF;
						align-items: center;

						.text_8 {
							margin-left: 15rpx;
						}
					}

					.hoverClass {
						background-color: rgba(216, 88, 134, 0.2);
						color: rgb(216, 88, 134);
						border: solid 4rpx rgb(216, 88, 134);
					}
				}
			}
		}

		.left-section {
			height: 240rpx;
			overflow: hidden;
			// border: solid 4rpx rgb(216, 88, 134);
			border-radius: 16px;

			.text_1 {
				margin-top: 12rpx;

			}
		}

		.section_use {
			border: solid 4rpx rgb(236, 236, 236);
			color: rgba(213, 213, 213, 1);
		}

		.equal-division-item_1 {
			width: 208rpx;
			color: rgb(216, 88, 134);
			font-size: 28rpx;
			font-weight: 600;
			line-height: 40rpx;
			padding: 4rpx 14rpx 6rpx;
			border-radius: 8rpx;
			height: 62rpx;
			border: solid 4rpx rgb(216, 88, 134);
			background-color: #FFFFFF;
			align-items: center;

			.image_6 {
				width: 40rpx;
				height: 38rpx;
			}

			.text_9 {
				margin-left: 16rpx;
			}
		}

		.hoverClass {
			background-color: rgba(216, 88, 134, 0.2);
			color: rgb(216, 88, 134);
			border: solid 4rpx rgb(216, 88, 134);
		}

		.image_1 {
			width: 180rpx;
			height: 194rpx;
			border-radius: 16rpx;
		}

		.image_4 {
			width: 40rpx;
			height: 40rpx;
		}
	}
}
</style>